﻿$(function () {
    $("#menudiv").hover(function () {
        $("#cent").show();
    },
    function () {
        $("#cent").hide();
    });

    $("#cenm .box #faq li").click(function () {
        $("#cenm .box #faq li dl dd").hide();
        $(this).find("dd").show();
    });
    $("#cenm .box #faq li dl dd .allsort .item").hover(function () {
        $(this).addClass("hover");
        $(this).find(".i-mc").show();
    },
    function () {
        $(this).removeClass("hover");
        $(this).find(".i-mc").hide();
    });

    $("#backtop").hover(function () {
        $("#backtopbanner").attr("src", OldImagePrefix + "/backtoph.png");
    }, function () {
        $("#backtopbanner").attr("src", OldImagePrefix + "/backtopq.png");
    });

    $("#backtop").click(function () {
        $("html,body").animate({ scrollTop: $("body").offset().top }, 500);
    });

    setTopAndLeft(null);
});

function CheckBoxAction(obj, stylename) {
    $("." + stylename).each(function (i) {
        //$(this).attr("checked", $(obj).attr("checked"));
        $(this).attr("checked", obj.checked);
    });
}

function Logout() {
    $.ajax({
        type: "POST",
        url: "/Handler/CommonHandler.ashx",
        data: "func=CustomerLogout",
        dataType: "json",
        success: function (jsonObj) {
            if (jsonObj.success) {
                //alert("已退出，欢迎继续选购我们的商品！");
                window.location = jsonObj.LogoutURL;
            } else {
                alert(jsonObj.msg);
            }

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
}

function doSearch() {
    var keywords = $("#txt_keywords").val();
    if (keywords == '请输入商品关键词或者货号') {
        document.getElementById('txt_keywords').value = '';
        keywords = '';
    }
    if ($.trim(keywords) == '') {
        alert("请输入搜索关键词！");
        return;
    }

    var sflag = false;
    var pattern = new RegExp("[`~!@#$^&*=+|{}':;'\\[\\].<>/?~@#……&*|{}]");
    for (var i = 0; i < keywords.length; i++) {
        if (pattern.test(keywords.substr(i, 1))) {
            sflag = true;
            break;
        }
        //rs = rs + keywords.substr(i, 1).replace(pattern, '|');
    }
//    var rs = "";
//    for (var i = 0; i < keywords.length; i++) {
//        rs = rs + keywords.substr(i, 1).replace(pattern, '|');
//    }
//    keywords = rs;
//    if (keywords.indexOf('|') >= 0) {
//        alert("搜索关键字中包含特殊字符！");
//        return;
//    }

    SetCookie("keyword", keywords);
    //window.location = gProductSiteURL + '/search.aspx?p=1&keyword=' + keywords;
    if (sflag) {
        window.location = gProductSiteURL + '/search.aspx?p=1&keyword=' + encodeURIComponent($.trim(keywords));
    } else {
        window.location = gSearchURL.replace(/keywordstring/g, encodeURIComponent($.trim(keywords)));
    }
    
    return false;
}

function SetCookie(name, value)//两个参数，一个是cookie的名子，一个是值
{
    var exp = new Date();    //new Date("December 31, 9998");
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

//将商品加入购物车
function AddProductToCart(pid, buycount) {
    $.ajax({
        type: "POST",
        url: "/Handler/ProductHandler.ashx",
        data: "func=buyingNow&InventoryItemID=" + pid +
                    "&Count=" + buycount,
        dataType: "json",
        success: function (jsonObj) {
            if (jsonObj.success) {
                setTimeout("GoToCart()", 500);
            } else {
                alert(jsonObj.msg);
            }

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
}

function GoToCart() {
    window.location = gCartURL;
}

//加入收藏夹
function setFavorite() {
    try {
        if (document.all) {

            window.external.AddFavorite('http://' + window.location.host, '风尚家庭购物');
        }
        else if (window.sidebar) {
            window.sidebar.AddPanel('风尚家庭购物', 'http://' + window.location.host, "");
        }
        else {
            alert('对不起，您的浏览器不支持此操作！请您使用菜单栏或Ctrl+D收藏本站。');
        }
    }
    catch (e) {
        alert('对不起，您的浏览器不支持此操作！请您使用菜单栏或Ctrl+D收藏本站。');
    }
}

function setTopAndLeft(pScrollY) {
    var ScrollY = document.documentElement.scrollTop || document.body.scrollTop; //兼容处理
    if (pScrollY == null) { pScrollY = 0; }
    var moveTop = ScrollY; //可调整移动速度
    moveTop = (moveTop > 0) ? Math.ceil(moveTop) : Math.floor(moveTop);
    if (moveTop > 500) {
        $("#backtop").fadeIn();
        pScrollY = moveTop;
    }
    else {
        $("#backtop").fadeOut();
        pScrollY = moveTop;
    }
    setTimeout("setTopAndLeft(" + pScrollY + ")", 50);
}

function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return ""; //返回参数值
}

/*广告追踪js代码 begin*/
function advtrackfun(advid, advitemid, url) {
    var brow = getbrowsername();
    var ip = "";
    try {
        ip = returnCitySN["cip"];
    } catch (ex) {
        $.getScript('http://counter.sina.com.cn/ip/');
        ip = ILData[0];
    }
    //异步提交追踪代码
    var userContent = {
        type: "POST",
        url: gHomeSiteURL + "/LoadAdv.aspx/InsertTrack",
        data: "{adv_id:" + advid + ",advitem_id:'" + advitemid + "',url:'" +
url + "',browser:'" + brow + "',ip:'" + ip + "'}",
        contentType: "application/json; charset:utf-8",
        dataType: "json",
        success: function (res) {
        }
    };
    $.ajax(userContent);
    //跳转页面
    window.open(url);
}

function getbrowsername() {
    var browserstr = navigator.userAgent;
    var flag = "";
    if (browserstr.indexOf('OPR') > 0) {
        flag = "Opera" + browserstr.substr(browserstr.indexOf('OPR') + 3);
    }
    else if (browserstr.indexOf('Maxthon') > 0) {
        flag = "Maxthon";
    }
    else if (browserstr.indexOf('Maxthon 2.0') > 0) {
        flag = "Maxthon 2.0";
    }
    else if (is360()) {
        flag = "360浏览器";
    }
    else if (browserstr.indexOf('TheWorld') > 0) {
        flag = browserstr.substr(browserstr.indexOf('TheWorld')); //世界之窗浏览器
    }
    else if (browserstr.indexOf('UBrowser') > 0) {
        flag = "UC浏览器"; //世界之窗浏览器
    }

    else if (browserstr.indexOf('Chrome') > 0) {
        flag = browserstr.substr(browserstr.indexOf('Chrome'), 18);
    }
    else if (browserstr.indexOf('Safari') > 0) {
        flag = browserstr.substr(browserstr.indexOf('Safari'));
    }
    else if (browserstr.indexOf('Firefox') > 0) {
        flag = browserstr.substr(browserstr.indexOf('Firefox'));
    }

    else if (browserstr.indexOf('MSIE') > 0) {
        var splitstr = browserstr.split(";");
        flag = splitstr[1];
    }
    else if (browserstr.indexOf('Netscape') > 0) {
        flag = browserstr.substr(browserstr.indexOf('Netscape'));
    }
    else if (browserstr.indexOf('Netscape') > 0) {
        flag = "其它浏览器";
    }

    return flag;
}
function is360() {
    var desc;
    var flag = false;
    if (navigator.userAgent.toLowerCase().indexOf("chrome") > -1) {
        desc = "qianzhui" + navigator.mimeTypes["application/x-shockwave-flash"].description.toLowerCase();
        if (desc.indexOf('adobe') > 0) {
            flag = true;
        }
    }
    return flag;
}
/*广告追踪js代码 end */